<script setup>
  import { Delete, Edit } from '@element-plus/icons-vue'
  import { ref } from 'vue'
  import ViewClass from '@/components/shared/ViewClass.vue'
  import { getClassListService, classDelService } from '@/api/user'
  const loading = ref(false)
  const drawer = ref()
  const classData = ref([])
  const pageSize =ref(15)
  const currentPage=ref(1)
  const openDrawer = () => {
    drawer.value.open({})
  }
  const onEdit = (row)=>{
    drawer.value.open(row)

  }
  const getClassList = async () => {
    loading.value = true
    const res = await getClassListService()
    classData.value = res.data
    loading.value = false
  }
  getClassList()

  const onDel = async (row) => {
    await ElMessageBox.confirm('你确认删除该班级信息吗？', '温馨提示', {
      type: 'warning',
      confirmButtonText: '确认',
      cancelButtonText: '取消'
    })
    await classDelService(row._id)
    ElMessage({ type: 'success', message: '删除成功' })
    getClassList()
  }

  const onSuccess = () => {
    getClassList()
  }


</script>

<template>
  <page-container title="班级管理">
    <template #extra>
      <el-button type="primary" @click="openDrawer">添加班级</el-button>
    </template>
    <el-table :data="classData" style="width: 100%" v-loading="loading">
      <el-table-column label="入学年份" prop="year"></el-table-column>
      <el-table-column label="专业名称" prop="major"></el-table-column>
      <el-table-column label="班级编号" prop="code"></el-table-column>
      <el-table-column label="学生人数" prop="count"></el-table-column>
      <el-table-column label="班主任">
        <template #default="{ row }">
          {{ row.teacher ? row.teacher.name : '——' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-button size="small" @click="onEdit(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="onDel(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <el-pagination background layout="total,prev, pager, next" :total="classData.length" style="margin-top: 20px; 
    justify-content: flex-end"/>
    <view-class ref="drawer" @success="onSuccess"></view-class>
  </page-container>

</template>

<style scoped></style>